﻿using politicsWorks.Forms;
using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;

namespace politicsWorks.UserControls
{
    public partial class UC_Teacher : UserControl
    {
        ArrayList arrayList = null;
        private DataTable dataTable;

        public UC_Teacher()
        {
            InitializeComponent();
            arrayList = new ArrayList();
            ReadStudentInfo();
        }

        private void ReadStudentInfo()
        {
            arrayList.Add(new StudentInfo("王旭东", "2018203895", 5, 4, 1));
            arrayList.Add(new StudentInfo("dasd", "2018203625", 5, 4, 1));
            arrayList.Add(new StudentInfo("dgsdg", "2018203234", 5, 4, 1));
            arrayList.Add(new StudentInfo("gdsg", "2018203654", 5, 4, 1));
            arrayList.Add(new StudentInfo("gdg", "2018203999", 5, 4, 1));
            BindingData();
        }

        public class StudentInfo
        {
            public string no;
            public string name;
            public int right = 0;
            public int wrong = 0;
            public int completeExam = 0;
            public StudentInfo(string name, string no)
            {
                this.name = name;
                this.no = no;
            }

            public StudentInfo(string name, string no, int right, int wrong, int completeExam) : this(name, no)
            {
                this.right = right;
                this.wrong = wrong;
                this.completeExam = completeExam;
            }

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            FormAddStudent addStudent = new FormAddStudent();
            addStudent.Show();
            addStudent.addStudent += AddStudent_addStudent;
        }

        private void AddStudent_addStudent(string sname, string sno)
        {
            AddStudentInfo(sname, sno);
        }

        public void AddStudentInfo(string sname, string sno)
        {
            StudentInfo info = new StudentInfo(sname, sno);
            arrayList.Add(info);
            BindingData();
        }

        public void BindingData()
        {
            dataTable = new DataTable();
            dataTable.Columns.Add("no", typeof(string));
            dataTable.Columns.Add("name", typeof(string));
            dataTable.Columns.Add("right", typeof(int));
            dataTable.Columns.Add("wrong", typeof(int));
            dataTable.Columns.Add("complete", typeof(int));
            dataTable.Columns.Add("percent", typeof(string));
            dataTable.Columns.Add("completeExam", typeof(int));
            for (int i = 0; i < arrayList.Count; i++)
            {
                StudentInfo info = (StudentInfo)arrayList[i];
                int total = (info.right + info.wrong);
                double percentD = (double)info.right / total * 100;
                string percent = percentD.ToString("0.00") + "%";
                if (total == 0)
                    percent = "0%";
                dataTable.Rows.Add(info.no, info.name, info.right, info.wrong,
                    total, percent, info.completeExam);
            }
            BindingSource source = new BindingSource();
            source.DataSource = dataTable;
            dataGridView1.DataSource = source;
        }
    }
}
